package cn.action.claim;

import cn.entity.ClaimVoucher;
import cn.entity.ClaimVoucherDetail;
import cn.entity.Employee;
import cn.service.claim.ClaimVoucherBiz;
import cn.service.claim.ClaimVoucherDetailBiz;
import cn.utils.Page;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import java.util.*;

public class ClaimVoucherAction extends ActionSupport {
    private String toMethod;
    private ClaimVoucher claimVoucher;
    private List<ClaimVoucherDetail> detailList;
    private ClaimVoucherBiz claimVoucherBiz;
    private ClaimVoucherDetailBiz claimVoucherDetailBiz;
    private Page pageSupport;
    private int pageNo;
    private int pageSize;
    private Date startDate;
    private Date endDate;
    private Map statusMap = new HashMap(){
        {
            put("新创建","新创建");put("已提交","已提交");put("待审核","待审核");
            put("已打回","已打回");put("已审批","已审批");put("已付款","已付款");
            put("已终止","已终止");
        }
    };

    /**
     * 跳转页面->新增报销单 claim_voucher_edit.jsp
     * @return
     */
    public String toAdd(){
        toMethod = "/jsp/claim/claim_voucher_edit.jsp";
        return SUCCESS;
    }

    /**
     * 保存报销单
     * @return
     */
    public String saveClaimVoucher(){
        Map session = ActionContext.getContext().getSession();
        Employee employee = (Employee) session.get("employee");
        claimVoucher.setCreateTime(new Date());
        claimVoucher.setCreator(employee);
        claimVoucher.setNextDeal(employee);
        claimVoucher.setDetailList(detailList);
        for (ClaimVoucherDetail claimVoucherDetail:detailList) {
            claimVoucherDetail.setBizClaimVoucher(claimVoucher);
        }
        claimVoucherBiz.saveOrUpdate(claimVoucher);
        toMethod="/claimVoucher_searchClaimVoucher.action";
        return "success-action";
    }

    /**
     * 查看报销单列表
     * @return
     */
    public String searchClaimVoucher(){
        Map session = ActionContext.getContext().getSession();
        Employee employee = (Employee) session.get("employee");
        pageSupport = (Page) session.get("pageSupport");
        if (pageSupport==null||pageNo==0){
            pageSupport = new Page<ClaimVoucher>();
            claimVoucher = new ClaimVoucher();
            pageNo=1;
            pageSize=5;
        }
        pageSupport.setCurrPageNo(pageNo);
        pageSupport.setPageSize(pageSize);
        pageSupport.setTotalCount(claimVoucherBiz.findCountBySn(employee,claimVoucher.getStatus(),startDate,endDate));
        pageSupport.setItems(claimVoucherBiz.findBySnAndPage(employee,pageNo,pageSize,claimVoucher.getStatus(),startDate,endDate));
        if (pageSupport.getItems().size()<1){
            pageSupport.setCurrPageNo(0);
            pageSupport.setTotalPageCount(0);
            pageSupport.setTotalCount(1111);
        }
        session.put("pageSupport",pageSupport);
        toMethod = "/jsp/claim/claim_voucher_list.jsp";
        return SUCCESS;
    }

    /**
     * 跳转页面->修改报销单 claim_voucher_update.jsp
     * @return
     */
    public String toUpdate(){
        claimVoucher = claimVoucherBiz.findById(claimVoucher.getId());
        toMethod="/jsp/claim/claim_voucher_update.jsp";
        return SUCCESS;
    }

    /**
     * 修改报销单
     * @return
     */
    public String updateClaimVoucher(){
        Map session = ActionContext.getContext().getSession();
        Employee employee = (Employee) session.get("employee");
        //查询前报销单
        ClaimVoucher claimVoucher1 = claimVoucherBiz.findById(claimVoucher.getId());
        claimVoucher.setCreator(employee);
        claimVoucher.setCreateTime(claimVoucher1.getCreateTime());
        claimVoucher.setModifyTime(new Date());
        claimVoucher.setNextDeal(employee);
        if (detailList==null){
            detailList = new ArrayList<>();
        }
        for (ClaimVoucherDetail claimVoucherDetail:detailList) {
            claimVoucherDetail.setBizClaimVoucher(claimVoucher);
        }

        claimVoucher.setDetailList(detailList);
        claimVoucherBiz.saveOrUpdate(claimVoucher);
        //查询更新后的报销单
        claimVoucher1 = claimVoucherBiz.findById(claimVoucher.getId());
        //保存要删除的报销单明细
        Map<Long,ClaimVoucherDetail> claimVoucherDetailMap = new HashMap<Long,ClaimVoucherDetail>();
        List<ClaimVoucherDetail> detailList1 = claimVoucher1.getDetailList();
        if (claimVoucher1.getDetailList().size()!=detailList.size()){
            //将所有的报销单明细存在map  key为 明细id
            for (ClaimVoucherDetail claimVoucherDetail:detailList1) {
                claimVoucherDetailMap.put(claimVoucherDetail.getId(),claimVoucherDetail);
            }
            //将不删除的明细 从map去掉
            for (ClaimVoucherDetail claimVoucherDetail:detailList) {
                if (claimVoucherDetailMap.containsKey(claimVoucherDetail.getId().longValue())){
                    claimVoucherDetailMap.remove(claimVoucherDetail.getId().longValue());
                }
            }

            for (Long key:claimVoucherDetailMap.keySet()) {
                claimVoucherDetailBiz.delete(claimVoucherDetailMap.get(key));
            }
        }
        toMethod="/claimVoucher_searchClaimVoucher.action";
        return "success-action";
    }

    /**
     * 查看报销单详情
     * @return
     */
    public String getClaimVoucherById(){
        claimVoucher = claimVoucherBiz.findById(claimVoucher.getId());
        toMethod="/jsp/claim/claim_voucher_view.jsp";
        return SUCCESS;
    }

    /**
     * 删除报销单
     * @return
     */
    public String deleteClaimVoucherById(){
        claimVoucher = claimVoucherBiz.findById(claimVoucher.getId());
        claimVoucherBiz.delete(claimVoucher);
        toMethod="/claimVoucher_searchClaimVoucher.action";
        return "success-action";
    }

    /**
     * 跳转页面 ->审核页面 claim_voucher_check.jsp
     * @return
     */
    public String toCheck(){
        claimVoucher =claimVoucherBiz.findById(claimVoucher.getId());
        toMethod="/jsp/claim/claim_voucher_check.jsp";
        return SUCCESS;
    }

    public String getToMethod() {
        return toMethod;
    }

    public void setToMethod(String toMethod) {
        this.toMethod = toMethod;
    }

    public ClaimVoucher getClaimVoucher() {
        return claimVoucher;
    }

    public void setClaimVoucher(ClaimVoucher claimVoucher) {
        this.claimVoucher = claimVoucher;
    }

    public List<ClaimVoucherDetail> getDetailList() {
        return detailList;
    }

    public void setDetailList(List<ClaimVoucherDetail> detailList) {
        this.detailList = detailList;
    }

    public ClaimVoucherBiz getClaimVoucherBiz() {
        return claimVoucherBiz;
    }

    public void setClaimVoucherBiz(ClaimVoucherBiz claimVoucherBiz) {
        this.claimVoucherBiz = claimVoucherBiz;
    }

    public Page getPageSupport() {
        return pageSupport;
    }

    public void setPageSupport(Page pageSupport) {
        this.pageSupport = pageSupport;
    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public void setStatusMap(Map statusMap) {
        this.statusMap = statusMap;
    }

    public Map getStatusMap() {

        return statusMap;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public ClaimVoucherDetailBiz getClaimVoucherDetailBiz() {
        return claimVoucherDetailBiz;
    }

    public void setClaimVoucherDetailBiz(ClaimVoucherDetailBiz claimVoucherDetailBiz) {
        this.claimVoucherDetailBiz = claimVoucherDetailBiz;
    }
}
